﻿Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Shapes
Imports System.Data.SqlClient

''' <summary>
''' Interaction logic for ConfigWindow.xaml
''' </summary>
Partial Public Class ConfigWindow
    Inherits Window
    Public Sub New()
        Me.Cursor = Cursors.Wait
        InitializeComponent()
        Me.Cursor = Cursors.Arrow
    End Sub

    Private Sub CloseCommand_Executed(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
        Me.Close()
    End Sub

    Private Sub Window_MouseDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
        Me.DragMove()
    End Sub

    Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Try
            Dim conStr As String = Settings.[Default].ConnectionStr
            Dim arrTemp As String() = conStr.Split(";"c)
            txtServerName.Text = arrTemp(0).Split("="c)(1)
            txtDatabaseName.Text = arrTemp(1).Split("="c)(1)

            txtUserName.Text = arrTemp(2).Split("="c)(1)
            pbPassword.Password = arrTemp(3).Split("="c)(1)
            SliderTimeout.Value = Convert.ToDouble(arrTemp(4).Split("="c)(1))
            txtWorkspace.Text = Settings.[Default].WorkSpacePath
        Catch generatedExceptionName As Exception
        End Try
    End Sub

    Private Sub btnSaveChanges_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim state As Boolean = False
        Dim errorText As String = ""
        Dim x As Integer = 0
        If Not (txtServerName.Text.Length > 0) Then
            errorText += "ServerName is required" & vbLf
            state = True
        End If
        If Not (txtDatabaseName.Text.Length > 0) Then
            errorText += "DataBase Name is required" & vbLf
            state = True
        End If

        If Not (txtUserName.Text.Length > 0) Then
            errorText += "UserName is required"
            state = True
        End If
        If Not (txtWorkspace.Text.Length > 0) Then
            errorText += "WorkSpace is required" & vbLf
            state = True
        End If
        Settings.[Default].WorkSpacePath = txtWorkspace.Text
        If state = False Then
            Try
                Dim conStr As String = ((("Data Source=" + txtServerName.Text & ";Initial Catalog=") + txtDatabaseName.Text & ";User Id=") + txtUserName.Text & ";Password=") + pbPassword.Password & ";Connection Timeout=" & SliderTimeout.Value.ToString() & ""
                Settings.[Default].ConnectionStr = conStr
                Settings.[Default].Save()
                Settings.[Default].Reload()
                'SILMInterfaces.DatabaseSettings.databaseName = txtDatabaseName.Text
                MessageBox.Show("Config has changed successfully. You need to launch the application again.")
                Me.DialogResult = True
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        Else
            MessageBox.Show(errorText, "Error", MessageBoxButton.OK, MessageBoxImage.[Error])
        End If
    End Sub

    Private Sub btnTest_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Me.Cursor = Cursors.Wait
        Dim state As Boolean = False
        Dim errorText As String = ""
        Dim x As Integer = 0
        If Not (txtServerName.Text.Length > 0) Then
            errorText += "ServerName is required" & vbLf
            state = True
        End If
        If Not (txtDatabaseName.Text.Length > 0) Then
            errorText += "DataBase Name is required" & vbLf
            state = True
        End If

        If Not (txtUserName.Text.Length > 0) Then
            errorText += "UserName is required"
            state = True
        End If

        If state = False Then
            Dim conStr As String = ((("Data Source=" + txtServerName.Text & ";Initial Catalog=") + txtDatabaseName.Text & ";User Id=") + txtUserName.Text & ";Password=") + pbPassword.Password & ";Connection Timeout=" & SliderTimeout.Value.ToString() & ""
            Dim con As New SqlConnection(conStr)
            Try
                con.Open()
                con.Close()
                MessageBox.Show(ErrorMessages.[Default].ConnectionTestSucceed)
            Catch generatedExceptionName As SqlException
                MessageBox.Show(ErrorMessages.[Default].ConnectionTestFailed, "Error", MessageBoxButton.OK, MessageBoxImage.[Error])

            End Try
        Else
            MessageBox.Show(errorText, "Error", MessageBoxButton.OK, MessageBoxImage.[Error])
        End If
        Me.Cursor = Cursors.Arrow
    End Sub
End Class
